import { PureComponent } from 'react';
import {
  Scene, OrthographicCamera, BoxGeometry, Line, Vector3,
  Mesh, WebGLRenderer, MeshBasicMaterial, AmbientLight,
  MeshLambertMaterial
} from 'three';

const drawAxes = scene => {
  const xGeo = new BoxGeometry();
  const xGeoPoints = [
    new Vector3(0, 0, 0),
    new Vector3(6, 0, 0)
  ];
  xGeo.setFromPoints(xGeoPoints);
  const xMat = new MeshBasicMaterial({
    color: 0xff0000
  });
  const xAxis = new Line(xGeo, xMat);
  scene.add(xAxis);

  const yGeo = new BoxGeometry();
  const yGeoPoints = [
    new Vector3(0, 0, 0),
    new Vector3(0, 6, 0)
  ];
  yGeo.setFromPoints(yGeoPoints);
  const yMat = new MeshBasicMaterial({
    color: 0x00ff00
  });
  const yAxis = new Line(yGeo, yMat);
  scene.add(yAxis);

  const zGeo = new BoxGeometry();
  const zGeoPoints = [
    new Vector3(0, 0, 0),
    new Vector3(0, 0, 6)
  ];
  zGeo.setFromPoints(zGeoPoints);
  const zMat = new MeshBasicMaterial({
    color: 0x00ccff
  });
  const zAxis = new Line(zGeo, zMat);
  scene.add(zAxis);
};

const scene = new Scene();

drawAxes(scene);

const camera = new OrthographicCamera(-5, 5, 3.75, -3.75, 0.1, 100);
camera.position.set(5, 15, 25);
camera.lookAt(0, 0, 0);

const light = new AmbientLight(0xcccccc);
scene.add(light);

const leftGeo = new BoxGeometry(2, 2, 2);
const leftGeoMat = new MeshLambertMaterial({
  color: 0xffffff
});
const leftMesh = new Mesh(leftGeo, leftGeoMat);
leftMesh.position.x = -3;
scene.add(leftMesh);

const rightGeo = new BoxGeometry(2, 2, 2);
const rightGeoMat = new MeshLambertMaterial({
  color: 0x00ff00
});
const rightMesh = new Mesh(rightGeo, rightGeoMat);
rightMesh.position.x = 3;
scene.add(rightMesh);

/**
 * 如下两份代码效果一样:
 * 1. 白光和MeshLambertMaterial, 也就是:
 * new AmbientLight(0xffffff) + MeshLambertMaterial
 * 2. 白光/灰光和MeshBasicMaterial:
 * new AmbientLight(0xffffff)/new AmbientLight(0xcccccc) + MeshBasicMaterial
 * 
 * 因为光照对MeshBasicMaterial不起作用, 要想光照效果起作用应使用其他材质, 比如MeshLambertMaterial
 */

class Index extends PureComponent {

  componentDidMount() {
    const renderer = new WebGLRenderer({
      canvas: document.getElementById('mainCanvas')
    });
    renderer.render(scene, camera);

  }

  render() {

    return (
      <canvas
        id="mainCanvas"
        width="400"
        height="300"
      />
    );
  }
}

export default Index;